package com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.impl;

import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.BxgdReportInfoNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.viewobjects.BxgdReportInfoQueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author z
 * @date 2020-12-11
 */
@Repository
public class BxgdReportInfoNativeRepositoryImpl implements BxgdReportInfoNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(BxgdReportInfoNativeRepositoryImpl.class);


    @Override
    public List<BxgdReportInfoEntity> queryBxgdReportInfos(String loginUserId, String reportYear, Integer pageNo, Integer pageSize) {
        StringBuilder sql = new StringBuilder("select * from bxgd_report_info ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");

        if (!StringUtils.isEmpty(loginUserId)) {
            whereSql.append(" and CREATE_USER = :loginUserId");
        }
        if (!StringUtils.isEmpty(reportYear)) {
//            whereSql.append(" and year(createTime)-1 = year(create_time) = :createTime");
//            whereSql.append(" and year(DATE_ADD(CREATE_TIME, INTERVAL -1 YEAR)) = :createTime");
            whereSql.append(" and REPORT_YEAR = :reportYear order by report_year desc ,create_time desc ");
        }

        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), BxgdReportInfoEntity.class);

        StringBuilder countSql = new StringBuilder("select count(*) from bxgd_report_info ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(reportYear)) {
            query.setParameter("reportYear", reportYear);
            countQuery.setParameter("reportYear", reportYear);
        }
        if (!StringUtils.isEmpty(loginUserId)) {
            query.setParameter("loginUserId", loginUserId);
            countQuery.setParameter("loginUserId", loginUserId);
        }
        List<BxgdReportInfoEntity> total = query.getResultList();
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<BxgdReportInfoEntity> list = query.getResultList();

        return (null != list && list.size() > 0) ? list : null;
    }

    @Override
    public List<BxgdReportInfoEntity> queryBxgdReportInfosCount(String loginUserId, String reportYear) {
        StringBuilder sql = new StringBuilder("select * from bxgd_report_info ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");

        if (!StringUtils.isEmpty(loginUserId)) {
            whereSql.append(" and CREATE_USER = :loginUserId");
        }
        if (!StringUtils.isEmpty(reportYear)) {
//            whereSql.append(" and year(createTime)-1 = year(create_time) = :createTime");
//            whereSql.append(" and year(DATE_ADD(CREATE_TIME, INTERVAL -1 YEAR)) = :createTime");
            whereSql.append(" and REPORT_YEAR = :reportYear");
        }

        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), BxgdReportInfoEntity.class);

        StringBuilder countSql = new StringBuilder("select count(*) from bxgd_report_info ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(reportYear)) {
            query.setParameter("reportYear", reportYear);
            countQuery.setParameter("reportYear", reportYear);
        }
        if (!StringUtils.isEmpty(loginUserId)) {
            query.setParameter("loginUserId", loginUserId);
            countQuery.setParameter("loginUserId", loginUserId);
        }
        List<BxgdReportInfoEntity> total = query.getResultList();

        return (null != total && total.size() > 0) ? total : null;
    }


    @Override
    public List<BxgdReportInfoEntity> queryBxgdReportInfo(BxgdReportInfoQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from bxgd_report_info ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(queryParams.getHonestPtjob())) {
            whereSql.append(" and HONEST_PTJOB = :honestPtjob");
        }
        if (!StringUtils.isEmpty(queryParams.getSaveConsumeOne())) {
            whereSql.append(" and SAVE_CONSUME_ONE = :saveConsumeOne");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), BxgdReportInfoEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from bxgd_report_info ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(queryParams.getHonestPtjob())) {
            query.setParameter("honestPtjob", queryParams.getHonestPtjob());
            countQuery.setParameter("honestPtjob", queryParams.getHonestPtjob());
        }
        if (!StringUtils.isEmpty(queryParams.getSaveConsumeOne())) {
            query.setParameter("saveConsumeOne", queryParams.getSaveConsumeOne());
            countQuery.setParameter("saveConsumeOne", queryParams.getSaveConsumeOne());
        }
        return query.getResultList();
    }

    @Override
    public List<BxgdReportInfoEntity> getBxgdReportInfo(String loginUserId) {
        StringBuilder sql = new StringBuilder("select * from bxgd_report_info ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(loginUserId)) {
            whereSql.append(" and CREATE_USER = :loginUserId order by create_time desc");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), BxgdReportInfoEntity.class);

        StringBuilder countSql = new StringBuilder("select count(*) from bxgd_report_info ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(loginUserId)) {
            query.setParameter("loginUserId", loginUserId);
            countQuery.setParameter("loginUserId", loginUserId);
        }
        List<BxgdReportInfoEntity> list = query.getResultList();
        return (null != list && list.size()>0) ? list :null;
    }
}
